package com.administrator.customviewtest.view.search_trans;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.Scroller;
import android.widget.TextView;

import com.administrator.customviewtest.R;
import com.administrator.customviewtest.view.test.TextView11;

import java.util.Arrays;
import java.util.List;

/**
 * Created by Administrator on 2017/2/4.
 */

public class SearchFirstAct extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<String> list;
    private ViewHolder viewHolder;
    private LinearLayoutManager linearLayoutManager;
    private Scroller scroller;

    private LinearLayout linearLayout;
    private TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_1);

        linearLayout = (LinearLayout) findViewById(R.id.ll);
        textView = (TextView) findViewById(R.id.tv);

        linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Log.e("ssss ll",linearLayout.getMeasuredWidth()+" "+linearLayout.getMeasuredHeight()+
                        " "+linearLayout.getWidth()+" "+linearLayout.getHeight()+" "+linearLayout.getLeft()+" "+linearLayout.getRight()
                +" "+linearLayout.getTop()+" "+linearLayout.getBottom()+" "+linearLayout.getX());

//                linearLayout.scrollTo(-100,-100);
            }
        });

        textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Log.e("ssss tv",textView.getMeasuredWidth()+" "+textView.getMeasuredHeight()+
                        " "+textView.getWidth()+" "+textView.getHeight()+" "+textView.getLeft()+" "+textView.getRight()
                        +" "+textView.getTop()+" "+textView.getBottom()+" "+textView.getX());
            }
        });







        recyclerView = (RecyclerView) findViewById(R.id.rv);

        list = Arrays.asList("a", "a", "a", "a", "a", "a",
                "b", "b", "b", "b", "b", "b",
                "c", "c", "c", "c", "c", "c",
                "d", "d", "d", "d", "d", "d",
                "e", "e", "e", "e", "e", "e",
                "f", "f", "f", "f", "f", "f");

        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public int getItemViewType(int position) {
                return super.getItemViewType(position);
            }

            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                viewHolder = new ViewHolder(LayoutInflater.from(SearchFirstAct.this).inflate(R.layout.item_tv, parent, false));
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                ((ViewHolder) holder).textView1.setText(list.get(position));
                ((ViewHolder) holder).textView2.setText(list.get(position));

                if (position == 0 || !list.get(position).equals(list.get(--position))) {
                    ((ViewHolder) holder).textView2.setVisibility(View.VISIBLE);
                } else {
                    ((ViewHolder) holder).textView2.setVisibility(View.GONE);
                }
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });


        findViewById(R.id.tv_search_bg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchFirstAct.this, SearchSecondAct.class);
                int location[] = new int[2];
                findViewById(R.id.tv_search_bg).getLocationOnScreen(location);
                intent.putExtra("x", location[0]);
                intent.putExtra("y", location[1]);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });
        ((TextView11) findViewById(R.id.test)).setOnWorldClick(new TextView11.onWorldClick() {
            @Override
            public void onClick(String s) {
                for (int i = 0; i < list.size(); i++) {
                    if (s.equals(list.get(i))) {
//                        linearLayoutManager.smoothScrollToPosition(recyclerView,state,i);
                        recyclerView.scrollToPosition(i);
//                        linearLayoutManager.scrollToPositionWithOffset(i,recyclerView.getHeight());
//                        recyclerView.smoothScrollBy(0,recyclerView.getChildAt(i).getTop());

                        break;
                    }
                }
                ((TextView) findViewById(R.id.tv_search_bg)).setText(s);

            }
        });
    }

    public void button1(View view) {
        scroller = new Scroller(this);
        scroller.startScroll(0,0,100,0);
        view.invalidate();
    }

    public void button2(View view) {
        view.invalidate();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView1;
        private TextView textView2;


        public ViewHolder(View itemView) {
            super(itemView);
            textView1 = (TextView) itemView.findViewById(R.id.item_tv);
            textView2 = (TextView) itemView.findViewById(R.id.item_tv1);

        }
    }

}
